<%@ page import="java.net.URLDecoder" %><%--
  Created by IntelliJ IDEA.
  User: ASUS
  Date: 2021/10/29
  Time: 23:18
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>登录</title>
    <script src="${pageContext.request.contextPath}/resources/js/jquery-1.10.1.min.js"></script>
    <script src="${pageContext.request.contextPath}/resources/js/bootstrap.min.js"></script>
    <link rel="stylesheet" href="${pageContext.request.contextPath}/resources/css/bootstrap.min.css">
</head>
<style>
    .form-group {
        width: 400px;
        margin: 0 auto 20px auto;
        position: relative;
    }

    .form-control {
        display: inline-block;
    }

    .error-message {
        color: #39C5BB;
        position: absolute;
    }

    .btn.btn-primary:hover {
        background-color: #206863;
    }

    .btn.btn-primary {
        width: 100%;
        color: white;
        background-color: #39C5BB;
        border-color: teal;
    }

    a {
        color: #00a1d6;
    }

    a:hover {
        color: #00a1d6;
        text-decoration: none;
    }
</style>
<body>
<div>
    <div style="text-align: center;">
        <legend style="font-size: 40px;">登录</legend>
    </div>
    <%
        request.setCharacterEncoding("utf-8");
        String username = "";
        String password = "";
        String checked = "";
        Cookie[] cookies = request.getCookies();
        if (cookies != null && cookies.length > 0) {
            for (Cookie cookie : cookies) {
                if (cookie.getName().equals("user")){
                    String user = URLDecoder.decode(cookie.getValue(),"utf-8");
                    username = user.substring(user.indexOf("=")+1);
                    checked = "checked";
                    break;
                }
            }
        }
    %>
    <div>
        <form action="${pageContext.request.contextPath}/sys/user/userLogin" method="POST" role="form">
            <div class="form-group">
                <input type="text" class="form-control" name="user.username" id="username" value="<%=username%>" placeholder="用户名">
            </div>
            <div class="form-group">
                <input type="password" class="form-control" name="user.password" id="password" value="<%=password%>" placeholder="密码">
                <p class="error-message"></p>
                <input type="checkbox" name="rememberPW" id="rememberPW" value="true" <%=checked%>>
                <p style="display: inline-block">记住用户名</p>
                <p><a href="" style="position: absolute;right: 0">忘记密码?</a></p>
            </div>
            <div class="form-group">
                <button type="button" id="submit" class="btn btn-primary">登录</button>
            </div>
            <div class="form-group">
                <a href="${pageContext.request.contextPath}/sys/user/register" style="position: absolute;right: 0;">没有账号?点击注册&gt;</a>
            </div>
        </form>
    </div>
</div>
</body>
<script>
    $('#submit').click(function () {
        $.post("${pageContext.request.contextPath}/sys/user/userLogin", {
                'user.username': $('#username').val(),
                'user.password': $('#password').val(),
                rememberPW: $('#rememberPW:checked').val()
            },
            function (data, textStatus, jqXHR) {
                if (data === "true") {
                    alert("登录成功");
                    window.location.replace("${pageContext.request.contextPath}/comics/index");
                } else {
                    $('#password + p').html("用户名或密码错误");
                }
            }
        );
    });
    $('input').bind('input', function () {
        $('#password + p').html("");
    });
</script>
</html>
